<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生" prop="doctorId">
        <el-select
          v-model="queryParams.doctorId"
          placeholder="请选择医生"
          clearable
        >
          <el-option
            v-for="doctor in doctorList"
            :key="doctor.id"
            :label="doctor.name"
            :value="doctor.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="疾病" prop="diseaseId">
        <el-select
          v-model="queryParams.diseaseId"
          placeholder="请选择疾病"
          clearable
        >
          <el-option
            v-for="disease in diseaseList"
            :key="disease.id"
            :label="disease.name"
            :value="disease.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['hospital:doctordisease:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['hospital:doctordisease:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['hospital:doctordisease:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['hospital:doctordisease:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="doctordiseaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="医生" align="center" prop="doctorName" />
      <el-table-column label="疾病" align="center" prop="diseaseName" />
      <el-table-column label="删除标记" align="center" prop="del" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['hospital:doctordisease:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['hospital:doctordisease:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改医生专病关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生" prop="doctorId">
          <el-select v-model="form.doctorId" placeholder="请选择医生">
            <el-option v-for="doctor in doctorList" :key="doctor.id" :label="doctor.name" :value="doctor.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="疾病" prop="diseaseId">
          <el-select v-model="form.diseaseId" placeholder="请选择疾病">
            <el-option v-for="disease in diseaseList" :key="disease.id" :label="disease.name" :value="disease.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="删除标记" prop="del">
          <el-input v-model="form.del" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDoctordisease, getDoctordisease, delDoctordisease, addDoctordisease, updateDoctordisease } from "@/api/hospital/doctordisease"
import { listDoctor } from "@/api/doctor/doctor"
import { listDisease } from "@/api/hospital/disease"

export default {
  name: "Doctordisease",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      doctordiseaseList: [],
      doctorList: [],
      diseaseList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: null,
        diseaseId: null,
        del: null
      },
      form: {},
      rules: {
        doctorId: [{ required: true, message: "医生不能为空", trigger: "blur" }],
        diseaseId: [{ required: true, message: "疾病不能为空", trigger: "blur" }],
        del: [{ required: true, message: "删除标记不能为空", trigger: "blur" }]
      }
    }
  },
  created() {
    this.getList()
    this.fetchDoctorList()
    this.fetchDiseaseList()
  },
  methods: {
    fetchDoctorList() {
      listDoctor({ del: 0 }).then(res => {
        this.doctorList = res.rows
      })
    },
    fetchDiseaseList() {
      listDisease({ del: 0 }).then(res => {
        this.diseaseList = res.rows
      })
    },
    getList() {
      this.loading = true
      listDoctordisease(this.queryParams).then(response => {
        this.doctordiseaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = { id: null, doctorId: null, diseaseId: null, del: null }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加医生专病关联"
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDoctordisease(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改医生专病关联"
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDoctordisease(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addDoctordisease(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除医生专病关联编号为"' + ids + '"的数据项？').then(() => {
        return delDoctordisease(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    handleExport() {
      this.download('hospital/doctordisease/export', { ...this.queryParams }, `doctordisease_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
